package com.ydcycloud.user.controller.wap;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.util.StringUtils;
import com.ydcycloud.base.bean.CommonResult;
import com.ydcycloud.base.enums.CommonEnums;
import com.ydcycloud.sys.controller.BaseWapController;
import com.ydcycloud.user.model.User;
import com.ydcycloud.user.service.UserService;

@Controller
@RequestMapping("/wap/user")
public class UserWapController extends BaseWapController {

	@Resource
	private UserService userService;

	/**
	 * 用户修改密码
	 * @param userId 用户id
	 * @param password 原密码
	 * @param newPassword 重置新密码
	 * @return
	 */
	@RequestMapping(path = "/changePassword", method = RequestMethod.POST)
	public @ResponseBody
	CommonResult changePassword(Integer userId, String password,
			String newPassword) {
		CommonResult r = new CommonResult();
		try {
			if (userId == null || StringUtils.isBlank(password)
					|| StringUtils.isBlank(newPassword)) {
				commonEnums = CommonEnums.PARANETER_ERROR; // 302 参数错误
			} else {
				User user = userService.get(userId);
				if (!user.getPassword().equals(password)) {
					commonEnums = CommonEnums.ORIGINAL_PWD; // 104 原密码不正确
				} else {
					user.setPassword(newPassword); // 密码
					user.setLastUpdateTime(new Date()); // 最后一次更新时间
					userService.save(user);
					commonEnums = CommonEnums.SUCCESS; // 300 成功
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			commonEnums = CommonEnums.SYSTEM_ERROR; // 301 系统错误
		} finally {
			r.setStatus(commonEnums.getValue());
			r.setMessage(commonEnums.getLabel());
		}
		return r;
	}
	
	/**
	 * 获取用户信息
	 * @return
	 */
	@RequestMapping(path = "/getUserInfo", method = RequestMethod.POST)
	public @ResponseBody
	CommonResult getUserInfo(HttpServletRequest request) {
		CommonResult r = new CommonResult();
		try {
				Integer userId = (Integer)request.getSession().getAttribute("userId");
				User bean = userService.get(userId);
				r.setData(bean);
				commonEnums = CommonEnums.SUCCESS; // 300 成功
		} catch (Exception e) {
			e.printStackTrace();
			commonEnums = CommonEnums.SYSTEM_ERROR; // 301 系统错误
		} finally {
			r.setStatus(commonEnums.getValue());
			r.setMessage(commonEnums.getLabel());
		}
		return r;
	}
	
	/**
	 * 保存/编辑用户信息
	 * @param realName 用户姓名
	 * @param sex 性别
	 * @param idCard 身份证号
	 * @param phone 手机号
	 * @param areaCode 地区码
	 * @param education 学历
	 * @param headImageUrl 头像
	 * @param wantJob 意愿工作
	 * @param expTime 工作经验
	 * @param expDetail 工作经历
	 * @return
	 */
	@RequestMapping("/saveUserInfo")
	public @ResponseBody CommonResult saveUserInfo(HttpServletRequest request,String realName,Integer sex,String idCard,String phone,String areaCode,
			Integer education,String headImageUrl,Integer wantJob,Integer expTime,String expDetail) {
		CommonResult r = new CommonResult();
		try {
			if(StringUtils.isBlank(idCard) || StringUtils.isBlank(realName) || sex==null
					 || StringUtils.isBlank(phone) || StringUtils.isBlank(areaCode) || education==null
					 || StringUtils.isBlank(headImageUrl) ||wantJob==null || expTime==null || StringUtils.isBlank(expDetail)){
				commonEnums = CommonEnums.PARANETER_ERROR;//302 参数错误
				return r;
			}
			Integer userId = (Integer)request.getSession().getAttribute("userId");
			User bean = userService.get(userId);
			bean.setName(realName);
			bean.setSex(sex);
			bean.setPhone(phone);
			bean.setIdCard(idCard);
			bean.setArea(areaCode);
			bean.setEducatin(education);
			bean.setHeadImageUrl(headImageUrl);
			bean.setWantJobId(wantJob);
			bean.setExpTime(expTime);
			bean.setExpDetail(expDetail);
			bean.setLastUpdateTime(new Date());
			userService.update(bean);
			commonEnums = CommonEnums.SUCCESS;//300 成功
		}catch (Exception e) {
			e.printStackTrace();
			commonEnums = CommonEnums.SYSTEM_ERROR; // 301 系统错误
		} finally {
			r.setStatus(commonEnums.getValue());
			r.setMessage(commonEnums.getLabel());
		}
		return r;
	}
	
	/**
	 * 证件上传
	 * @param request
	 * @param cardUrl 证件路径
	 * @return
	 */
	@RequestMapping("/cardUpload")
	public @ResponseBody CommonResult cardUpload(HttpServletRequest request,String cardUrl) {
		CommonResult r = new CommonResult();
		try {
			if(StringUtils.isBlank(cardUrl)){
				commonEnums = CommonEnums.PARANETER_ERROR;//302 参数错误
				return r;
			}else{
				Integer userId = (Integer)request.getSession().getAttribute("userId");
				User bean = userService.get(userId);
				
				bean.setCardUrl(cardUrl);
				bean.setLastUpdateTime(new Date());
				userService.update(bean);
				commonEnums = CommonEnums.SUCCESS;//300 成功
			}
		}catch (Exception e) {
			e.printStackTrace();
			commonEnums = CommonEnums.SYSTEM_ERROR; // 301 系统错误
		} finally {
			r.setStatus(commonEnums.getValue());
			r.setMessage(commonEnums.getLabel());
		}
		return r;
	}
	
	
}
